const express = require('express')
const app = express()
const port = 3000

app.use(express.static('public')) // 静态资源托管
app.get('/', (req, res) => res.send('Hello World!'))

//实现防盗链功能
app.use((req, res, next) => {
    if (req.headers.referer) {
        let referer = req.headers.referer
        if (referer.indexOf('127.0.0.1') == -1) {
            res.send('非法访问')
        } else {
            next()
        }
    } else {
        next()
    }
})
app.get('/img', (req, res) => res.send('http://127.0.0.1:3000/img/1.jpg'))



// 使用模板引擎, 使用的比较少,类似jsp
const path = require('path')
app.set('view engine', 'ejs') // 设置模板引擎
app.set('views', path.resolve(__dirname,'./views')) // 设置模板文件路径
app.get('/home', (req, res) => res.render('index', { title: 'Hello World!', list: [1, 2, 3, 4, 5]}))





// 监听端口
app.listen(port, () => console.log(`Server running at  http://127.0.0.1:${port}`))
